package com.tikou.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Tikou on 2017/11/23.
 * 作者：TianNuo
 * 邮箱：1320917731@qq.com
 * 说明：饼状图
 * 参考：https://juejin.im/post/5a12303af265da432240cc0f
 * https://github.com/razerdp/AnimatedPieView
 */

public class PieView extends View {
    private Paint mPaint1, mPaint2, mPaint3, mPaint4;
    private RectF mDrawRectf = new RectF();

    public PieView(Context context) {
        this(context, null);
    }

    public PieView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint1.setStyle(Paint.Style.STROKE);
        mPaint1.setStrokeWidth(80);
        mPaint1.setColor(Color.RED);
        mPaint2 = new Paint(mPaint1);
        mPaint2.setColor(Color.GREEN);
        mPaint3 = new Paint(mPaint1);
        mPaint3.setColor(Color.BLUE);
        mPaint4 = new Paint(mPaint1);
        mPaint4.setColor(Color.YELLOW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final float width = getWidth() - getPaddingLeft() - getPaddingRight();
        final float height = getHeight() - getPaddingTop() - getPaddingBottom();
        canvas.translate(width / 2, height / 2);
        final float radius = (float) (Math.min(width, height) / 2 * 0.85);
        mDrawRectf.set(-radius, -radius, radius, radius);
        canvas.drawRect(mDrawRectf, mPaint4);
        canvas.drawArc(mDrawRectf, 0, 120, false, mPaint1);
        canvas.drawArc(mDrawRectf, 120, 120, false, mPaint2);
        canvas.drawArc(mDrawRectf, 240, 120, false, mPaint3);
    }
}
